package com.haisen.controller;


import com.haisen.core.utils.PageResultSet;
import com.haisen.core.utils.Result;
import com.haisen.dataobject.entity.CallOut;
import com.haisen.dataobject.query.CallOutQuery;
import com.haisen.service.CallOutService;
import com.haisen.service.DicService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping("callout")
@Slf4j
public class CallOutController {

    @Autowired
    private CallOutService callOutService;
    @Autowired
    private DicService dicService;


    @GetMapping
    @RequiresPermissions("callout:view")
    public String page(Model model){

        setCommonData(model);
        return "customer/callout";
    }

    @ResponseBody
    @RequiresPermissions("callout:view")
    @RequestMapping("/list")
    public PageResultSet<CallOut> list(CallOutQuery callOutQuery){
        return callOutService.findByPage(callOutQuery);
    }


    @ResponseBody
    @RequiresPermissions("callout:create")
    @PostMapping("/create")
    public Result create(@Validated CallOut callOut){
        callOutService.create(callOut);
        return Result.success();
    }


    @PostMapping("/update")
    @RequiresPermissions("callout:update")
    @ResponseBody
    public  Result update(@Validated CallOut callOut){
        callOutService.update(callOut);
        return Result.success();
    }

    @PostMapping("/delete")
    @RequiresPermissions("callout:delete")
    @ResponseBody
    public Result delete(@RequestParam("id") Long[] ids ){
        callOutService.delete(ids);
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("callout:import")
    @PostMapping("/import")
    public Result importExcel(@RequestParam("importpathfile") String pathFile) {
        callOutService.importExcel2DataBase(pathFile);
        return Result.success();
    }


    private void setCommonData(Model model) {
        model.addAttribute("brandMap", dicService.GetDic("tb_call_out", "brand"));
        model.addAttribute("handerResultMap", dicService.GetDic("tb_call_out", "handerResult"));


    }
}
